<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable>
          <el-option
            v-for="dict in dict.type.ord_order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"


          />
        </el-select>
      </el-form-item>
      <el-form-item label="付款金额" prop="payment">
        <el-input
          v-model="queryParams.payment"
          placeholder="请输入付款金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提交时间" prop="submitTime">
        <el-date-picker clearable
          v-model="queryParams.submitTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择提交时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="支付时间" prop="paymentTime">
        <el-date-picker clearable
          v-model="queryParams.paymentTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择支付时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="发货时间" prop="sendTime">
        <el-date-picker clearable
          v-model="queryParams.sendTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发货时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="收货时间" prop="deliveryTime">
        <el-date-picker clearable
          v-model="queryParams.deliveryTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择收货时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['order:orderBy:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:orderBy:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:orderBy:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:orderBy:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderByList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="orderId" />
      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ord_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="商家id" align="center" prop="businessId" />
      <el-table-column label="商品id" align="center" prop="productId" />
      <el-table-column label="付款金额" align="center" prop="payment" />
      <el-table-column label="优惠卷id" align="center" prop="preferentialId" />
      <el-table-column label="订单金额" align="center" prop="money" />
      <el-table-column label="订单编号" align="center" prop="number" />
      <el-table-column label="购物份数" align="center" prop="total" />
      <el-table-column label="订单详情id" align="center" prop="detailsId" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="提交时间" align="center" prop="submitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submitTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="paymentTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paymentTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发货时间" align="center" prop="sendTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货时间" align="center" prop="deliveryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:orderBy:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:orderBy:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.ord_order_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="商家id" prop="businessId">
          <el-input v-model="form.businessId" placeholder="请输入商家id" />
        </el-form-item>
        <el-form-item label="商品id" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="付款金额" prop="payment">
          <el-input v-model="form.payment" placeholder="请输入付款金额" />
        </el-form-item>
        <el-form-item label="优惠卷id" prop="preferentialId">
          <el-input v-model="form.preferentialId" placeholder="请输入优惠卷id" />
        </el-form-item>
        <el-form-item label="订单金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入订单金额" />
        </el-form-item>
        <el-form-item label="订单编号" prop="number">
          <el-input v-model="form.number" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="购物份数" prop="total">
          <el-input v-model="form.total" placeholder="请输入购物份数" />
        </el-form-item>
        <el-form-item label="订单详情id" prop="detailsId">
          <el-input v-model="form.detailsId" placeholder="请输入订单详情id" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="提交时间" prop="submitTime">
          <el-date-picker clearable
            v-model="form.submitTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择提交时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付时间" prop="paymentTime">
          <el-date-picker clearable
            v-model="form.paymentTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发货时间" prop="sendTime">
          <el-date-picker clearable
            v-model="form.sendTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="收货时间" prop="deliveryTime">
          <el-date-picker clearable
            v-model="form.deliveryTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择收货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">订单详情信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddDetails">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteDetails">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="detailsList" :row-class-name="rowDetailsIndex" @selection-change="handleDetailsSelectionChange" ref="details">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="订单编号" prop="orderNumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.orderNumber" placeholder="请输入订单编号" />
            </template>
          </el-table-column>
          <el-table-column label="商品数量" prop="orderTotal" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.orderTotal" placeholder="请输入商品数量" />
            </template>
          </el-table-column>
          <el-table-column label="订单金额" prop="orderMoney" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.orderMoney" placeholder="请输入订单金额" />
            </template>
          </el-table-column>
          <el-table-column label="配送员" prop="deliveryName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.deliveryName" placeholder="请输入配送员" />
            </template>
          </el-table-column>
          <el-table-column label="配送员电话" prop="deliveryPhone" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.deliveryPhone" placeholder="请输入配送员电话" />
            </template>
          </el-table-column>
          <el-table-column label="配送地址" prop="deliveryAddress" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.deliveryAddress" placeholder="请输入配送地址" />
            </template>
          </el-table-column>
          <el-table-column label="用户id" prop="userId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.userId" placeholder="请输入用户id" />
            </template>
          </el-table-column>
          <el-table-column label="商品id" prop="productId" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.productId" placeholder="请输入商品id" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrderBy, getOrderBy, delOrderBy, addOrderBy, updateOrderBy } from "@/api/order/orderBy";

export default {
  name: "OrderBy",
  dicts: ['ord_order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedDetails: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderByList: [],
      // 订单详情表格数据
      detailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        payment: null,
        number: null,
        submitTime: null,
        paymentTime: null,
        sendTime: null,
        deliveryTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [
          { required: true, message: "订单状态不能为空", trigger: "change" }
        ],
        productId: [
          { required: true, message: "商品id不能为空", trigger: "blur" }
        ],
        payment: [
          { required: true, message: "付款金额不能为空", trigger: "blur" }
        ],
        preferentialId: [
          { required: true, message: "优惠卷id不能为空", trigger: "blur" }
        ],
        money: [
          { required: true, message: "订单金额不能为空", trigger: "blur" }
        ],
        number: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        total: [
          { required: true, message: "购物份数不能为空", trigger: "blur" }
        ],
        detailsId: [
          { required: true, message: "订单详情id不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        submitTime: [
          { required: true, message: "提交时间不能为空", trigger: "blur" }
        ],
        paymentTime: [
          { required: true, message: "支付时间不能为空", trigger: "blur" }
        ],
        sendTime: [
          { required: true, message: "发货时间不能为空", trigger: "blur" }
        ],
        deliveryTime: [
          { required: true, message: "收货时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrderBy(this.queryParams).then(response => {
        this.orderByList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        status: null,
        businessId: null,
        productId: null,
        payment: null,
        preferentialId: null,
        money: null,
        number: null,
        total: null,
        detailsId: null,
        userId: null,
        submitTime: null,
        paymentTime: null,
        sendTime: null,
        deliveryTime: null,
        notes: null
      };
      this.detailsList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrderBy(orderId).then(response => {
        this.form = response.data;
        this.detailsList = response.data.detailsList;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.detailsList = this.detailsList;
          if (this.form.orderId != null) {
            updateOrderBy(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrderBy(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + orderIds + '"的数据项？').then(function() {
        return delOrderBy(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 订单详情序号 */
    rowDetailsIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 订单详情添加按钮操作 */
    handleAddDetails() {
      let obj = {};
      obj.orderNumber = "";
      obj.orderTotal = "";
      obj.orderMoney = "";
      obj.deliveryName = "";
      obj.deliveryPhone = "";
      obj.deliveryAddress = "";
      obj.userId = "";
      obj.productId = "";
      this.detailsList.push(obj);
    },
    /** 订单详情删除按钮操作 */
    handleDeleteDetails() {
      if (this.checkedDetails.length == 0) {
        this.$modal.msgError("请先选择要删除的订单详情数据");
      } else {
        const detailsList = this.detailsList;
        const checkedDetails = this.checkedDetails;
        this.detailsList = detailsList.filter(function(item) {
          return checkedDetails.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleDetailsSelectionChange(selection) {
      this.checkedDetails = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('order/orderBy/export', {
        ...this.queryParams
      }, `orderBy_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
